package cn.com.scitc.soft2101.data_interface;

import cn.com.scitc.soft2101.dao.PaidDao;
import cn.com.scitc.soft2101.model.Paid;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/webInterface/paid/list")
public class PaidListInterface extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, Object> result = new HashMap<>();
        List<Paid> list = null;
        try {
            PaidDao paidDao = new PaidDao();
            list = paidDao.findAll();

            result.put("code", 1);
            result.put("msg", "查询成功");
            result.put("data", list);
        }
        catch (Exception er)  {
            er.printStackTrace();

            result.put("code", 0);
            result.put("msg", "查询失败");
        }
        finally {
            String jsonString = JSON.toJSONString(result);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.print(jsonString);
            writer.close();
        }
    }
}
